﻿using _2112A_2_MES._wangweihao;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.SuJiaHaos;
using _2112A_2_MES.SuJiaHaos.WareHouse;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace _2112A_2_MES.SuJiaHao
{
    [ApiExplorerSettings(GroupName = "苏佳豪")]
    public class WarehouseService : ApplicationService, IWarehouseService
    {
        /// <summary>
        /// 学生仓储
        /// </summary>
        private readonly IRepository<WmWarehouse, int> _warehouseRepository;


        public WarehouseService(IRepository<WmWarehouse, int> warehouseRepository)
        {
            _warehouseRepository = warehouseRepository;
        }
        /// <summary>
        /// 获取仓库设置
        /// </summary>
        /// <param name="warehouseCode">仓库编码</param>
        /// <param name="warehouseName">仓库名称</param>
        /// <returns>返回仓库设置的泛型集合</returns>
        public async Task<ReturnSelect<object>> GetWmWarehouseList(string? warehouseCode, string? warehouseName, int pageSize, int pageIndex)
        {
            // 获取仓库列表
            var wareHouseList = await _warehouseRepository.GetListAsync();

            // 使用 WhereIf 进行条件筛选
            var wareHouseLists = wareHouseList
                .WhereIf(!string.IsNullOrEmpty(warehouseCode), x => x.WarehouseCode.Contains(warehouseCode))
                .WhereIf(!string.IsNullOrEmpty(warehouseName), x => x.WarehouseName.Contains(warehouseName))
                .ToList();

            // 计算总记录数和当前页记录
            var totalCount = wareHouseLists.Count;
            var pageList = wareHouseLists.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var result = AutoMapperExt.MapToList<WmWarehouse>(pageList);

            // 返回结果
            var data = new { TotalCount = totalCount, PageList = result };
            return new ReturnSelect<object>(true, "0", result != null && result.Count > 0 ? data : null);
        }

        /// <summary>
        /// 仓库信息添加
        /// </summary>
        /// <param name="input">仓库实体</param>
        /// <returns></returns>
        public async Task<int> GetCreateWmWarehouse(CreateWareHuoseDTO input)
        {
            var wmWareHouse = ObjectMapper.Map<CreateWareHuoseDTO, WmWarehouse>(input);
            wmWareHouse.CreateBy = "崔二强";
            wmWareHouse.CreateTime = DateTime.Now;
            await _warehouseRepository.InsertAsync(wmWareHouse);
            return wmWareHouse != null ? 1 : 0;

        }

        /// <summary>
        /// 仓库信息删除
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteWmWarehouse(int id)
        {
            var wmWareHouseDel =await _warehouseRepository.GetAsync(id);
            await _warehouseRepository.DeleteAsync(wmWareHouseDel);
            return wmWareHouseDel != null ? 1 : 0;
        }

        /// <summary>
        /// 仓库信息反填
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回仓库信息实体</returns>
        public async Task<WmWarehouse> GetWmWarehouse(int id)
        {
            var customers = await _warehouseRepository.FirstOrDefaultAsync(x => x.Id == id);
            return customers;
        }

        /// <summary>
        /// 仓库信息修改
        /// </summary>
        /// <param name="input">仓库实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateWmWarehouse(CreateWareHuoseDTO input)
        {
            var customers = ObjectMapper.Map<CreateWareHuoseDTO, WmWarehouse>(input);
            customers.UpdateBy = "崔大强";
            customers.UpdateTime = DateTime.Now;
            await _warehouseRepository.UpdateAsync(customers);
            return customers != null ? 1 : 0;
        }

    }
}
